<?php
namespace Admin\Controller;
//use Components;
use Think\Controller;
use Think\Page;
class CategoryController extends BaseController{
	//用于显示所有一级分类++++++++AJAX++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	public function createFirst(){
		$cid=$_POST['cid'];
		$data=D('Category')->where("cid='$cid'")->select();	
		$this->ajaxReturn($data);
	}

	//++++++++++++++++AJAX++++++++++++++++++AJAX+++++++++++++++AJAX+++++++++++++AJAX+++++++++++++++
	public function add(){
		$data=D('Category')->where('cid=0')->select();
		$this->assign('data',$data);
		$this->display('add');
	}
	public function addok(){
		if(IS_POST){
			$model=D('Category');
			$model->create();
			if($model->add()){
				$this->redirect('add',array(),2,'添加成功中...');
			}else{
				$this->error('添加失败',U('add'),2);
			}
		}
		
		
	}
	public function admin(){
		$model=D('Category');
	    $p=empty($_GET['p'])?1:$_GET['p'];   
	  
		//分页开始++++++++++++++++++++++++++++++++++++++++++++++++++++++
		$count=$model->count();//总记录数
		$page=new Page($count,8);//输入总记录数   每页分配的数量
		     //中间是留给更改样式的代码
		    $page->lastSuffix=false;//不显示最后页吗
		    $page->rollPage=5;//每页显示5个页码
		     $page->setConfig('first', '[首页]');
		     $page->setConfig('pre', '上一页');
		     $page->setConfig('next', '下一页');
		     $page->setConfig('last', '[末页]');
		 $page->setConfig('theme', '共%TOTAL_ROW% 条记录,当前是%NOW_PAGE%/%TOTAL_PAGE%  %FIRST%%UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
		     //中间是留给更改样式的代码
		$show=$page->show();
	    
	    $data=$model->relation(true)->order('id desc')->select();//分配数据-------注意---------注意-------注意--------
	    $data=gettree($data);//-------------------------------------注意-------注意-----------注意---------注意--------
	    //$data=limit($page->firstRow.','.$page->listRows);  等同与下面的式子
		$data=array_slice($data, $page->firstRow, $page->listRows);//------注意---------注意---注意-----注意--注意-----------
		//获得树状结构
		//load('@.tree');在3.2中不行
		//$tree=new \Components\Tree();
		//$data=$tree->getTree($data);  在common文件夹中定义了function.php这个文件
	    $this->assign('p',$p);
		$this->assign('show',$show);//分配给页脚
		$this->assign('data',$data);
		$this->display('admin');
	}
	//修改界面
	public function edit(){
		
		$p=$_GET['p'];
	
		 $id=$_GET['id'];
		 $model=D('Category');
		 $row=$model->find($id);
		 $data=$model->where('cid=0')->select();
		 
		 $this->assign('p',$p);
		 $this->assign('row',$row);
		 $this->assign('data',$data);
	     $this->display('edit');	
	}
	public function editok(){
		if(IS_POST){
			$p=$_POST['p'];
			$model=D('Category');
			$data=$model->create();       //注意创建的时候需要$data=$model->create();  下面就是覆盖了
			$data['name']=$_POST['name'];
			$data['content']=$_POST['content'];
			if($model->save($data)){
				$this->redirect('admin',array('p'=>$p),2,'修改成功...');
			}else{
				$this->redirect('edit',array(),2,'修改失败...');
			}
		}
	}
	//删除功能
	public function deleteAll(){
		if(isset($_POST['deleteSubmit'])){
			$id=$_POST['id'];
			$model=D('Category');
			/*for($i=0;$i<count($id);$i++){       这是第一种方法遍历id
				D('Category')->delete($id[$i]);
			}*/
			//implode是以逗号分隔并且连接起来；
			$where=implode(',', $id);
			if($model->delete($where)){
				$this->redirect('admin',array(),2,'删除成功...');
			}else{
				$this->error('删除失败',U('admin'),2);
			}
	
		}
	}
}